package com.rankeiot.core.util;


import javax.crypto.Mac;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;

/**
 * 签名工具
 */
public class SignUtil {
    private static final String HMAC_SHA1_ALGORITHM = "HmacSHA1";
    private static final String HMAC_MD5_ALGORITHM = "HmacMD5";
//    private static final String MAC_NAME = "HmacSHA1";
    private static final String ENCODING = "UTF-8";
//    public static String HmacSHA1Encrypt(String encryptText, String encryptKey) {
//        try {
//            byte[] data = encryptKey.getBytes(ENCODING);
//            // 根据给定的字节数组构造一个密钥,第二参数指定一个密钥算法的名称
//            SecretKey secretKey = new SecretKeySpec(data, MAC_NAME);
//            // 生成一个指定 Mac 算法 的 Mac 对象
//            Mac mac = Mac.getInstance(MAC_NAME);
//            // 用给定密钥初始化 Mac 对象
//            mac.init(secretKey);
//
//            byte[] text = encryptText.getBytes(ENCODING);
//            // 完成 Mac 操作
//            return Base64.getEncoder().encodeToString(mac.doFinal(text));
//        } catch (UnsupportedEncodingException | NoSuchAlgorithmException | InvalidKeyException e) {
//            throw new RuntimeException(e);
//        }
//    }
    public static String hmacMd5(String data, String key) {
        try {
            Mac hmacMD5 = Mac.getInstance(HMAC_MD5_ALGORITHM);
            SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes(ENCODING), HMAC_MD5_ALGORITHM);
            hmacMD5.init(secretKeySpec);
            byte[] encryptedBytes = hmacMD5.doFinal(data.getBytes(ENCODING));
            return Base64.getEncoder().encodeToString(encryptedBytes);
        } catch (NoSuchAlgorithmException | InvalidKeyException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
        return null;
    }
    /**
     * 使用 HMAC-SHA1 签名方法对data进行签名
     *
     * @param data
     *            被签名的字符串
     * @param key
     *            密钥
     * @return
    加密后的字符串
     */
    public static String hmacSha1(String data, String key) {
        try {
            //根据给定的字节数组构造一个密钥,第二参数指定一个密钥算法的名称
            SecretKeySpec signinKey = new SecretKeySpec(key.getBytes(ENCODING), HMAC_SHA1_ALGORITHM);
            //生成一个指定 Mac 算法 的 Mac 对象
            Mac mac = Mac.getInstance(HMAC_SHA1_ALGORITHM);
            //用给定密钥初始化 Mac 对象
            mac.init(signinKey);
            //完成 Mac 操作
            byte[] rawHmac = mac.doFinal(data.getBytes(ENCODING));
            return Base64.getEncoder().encodeToString(rawHmac);
        } catch (NoSuchAlgorithmException e) {
            System.err.println(e.getMessage());
        } catch (InvalidKeyException e) {
            System.err.println(e.getMessage());
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
        return null;
    }

//    public static void main(String[] args) throws Exception {
//        String stringToSign="GET&%2F&AccessKeyId%3Dtestid%26Action%3DDescribeDedicatedHosts%26Format%3DXML%26SignatureMethod%3DHMAC-SHA1%26SignatureNonce%3D3ee8c1b8-xxxx-xxxx-xxxx-xxxxxxxxx%26SignatureVersion%3D1.0%26Timestamp%3D2016-02-23T12%253A46%253A24Z%26Version%3D2014-05-26";
//        String accessKeySecret="testsecret&";
//        String out=com.aliyun.openapiutil.Client.getROASignature(stringToSign, accessKeySecret);
//        System.out.println(out);
//        //OLeaidS1JvxuMvnyHOwuJ+uX5qY=
//        System.out.println(HmacSHA1Encrypt(stringToSign,"testsecret&"));

//    }
}
